package com.ve.downloader.queue;

import java.util.Arrays;


public class QuickFixQueue implements FixQueue {

    private byte data[];
    private int start;

    @Override
    public boolean equalsArray(byte[] other) {
//        System.out.println(toString()+" ==  "+Arrays.toString(other));


        if (other.length != data.length) {
            return false;
        }
        for (int i = 0; i < data.length; i++) {
            if (get(i) != other[i]) {
                return false;
            }
        }
        return true;
    }

    public byte get(int index) {
        return data[(index + start) % data.length];
    }

    public QuickFixQueue(int length) {
        data = new byte[length];
    }

    @Override
    public void push(byte b) {
        data[start++] = b;
        start %= data.length;
    }

    @Override
    public byte[] getData() {
        byte[] bytes=new byte[data.length];
        for (int i = 0; i < bytes.length; i++) {
            bytes[i]=get(i);
        }
        return bytes;
    }

    @Override
    public String toString() {
        return Arrays.toString(getData());
    }

    @Override
    public void clear() {
        Arrays.fill(data, (byte) 0);
    }
}